#!/bin/bash

# ========================================
# 学生请假管理系统 - 开发环境启动脚本
# ========================================
# 功能: 初始化数据库 -> 启动后端 -> 启动前端
# 使用: bash start-dev.sh 或 ./start-dev.sh
# ========================================

set -e

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

echo ""
echo "========================================"
echo "学生请假管理系统 - 开发环境启动脚本"
echo "========================================"
echo ""

# ========================================
# 检查 Node.js 和 npm
# ========================================
echo "📋 检查环境..."

if ! command -v node &> /dev/null; then
  echo "❌ 未检测到 Node.js，请先安装 Node.js"
  exit 1
fi

NODE_VERSION=$(node -v)
NPM_VERSION=$(npm -v)

echo "✅ Node.js: $NODE_VERSION"
echo "✅ npm: $NPM_VERSION"
echo ""

# ========================================
# 检查环境变量文件
# ========================================
echo "🔐 检查环境配置..."

if [ ! -f "$SCRIPT_DIR/.env" ]; then
  echo "⚠️  前端 .env 文件不存在，创建默认配置..."
  echo "VITE_API_BASE_URL=http://localhost:8080/api" > "$SCRIPT_DIR/.env"
  echo "✅ 已创建 .env"
fi

if [ ! -f "$SCRIPT_DIR/server/.env" ]; then
  echo "❌ 后端 server/.env 文件不存在！"
  echo ""
  echo "请先创建 server/.env 文件并配置数据库信息:"
  echo "   DB_HOST=localhost"
  echo "   DB_PORT=3306"
  echo "   DB_USER=yepeiming99"
  echo "   DB_PASSWORD=你的密码"
  echo "   DB_NAME=SoftwareManagementExp"
  echo "   PORT=8080"
  echo "   NODE_ENV=development"
  echo "   JWT_SECRET=my-secret-key-for-student-leave-system-2025"
  echo ""
  read -p "已配置? (y/n): " -n 1 -r
  echo
  if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo "脚本已中止"
    exit 1
  fi
fi

echo "✅ 环境配置检查完成"
echo ""

# ========================================
# 步骤 1: 初始化数据库
# ========================================
echo "📊 步骤 1: 初始化数据库"
echo "----------------------------------------"

if [ -f "$SCRIPT_DIR/server/init-db.js" ]; then
  echo "运行数据库初始化脚本..."
  cd "$SCRIPT_DIR/server"
  node ../init-db.js
  cd "$SCRIPT_DIR"
  echo "✅ 数据库初始化完成"
else
  echo "⚠️  初始化脚本不存在，跳过数据库初始化"
fi

echo ""

# ========================================
# 步骤 2: 启动后端和前端
# ========================================
echo "🚀 步骤 2: 启动开发服务"
echo "----------------------------------------"
echo ""

# 创建清理函数，脚本退出时停止子进程
cleanup() {
  echo ""
  echo "🛑 正在停止服务..."
  kill $BACKEND_PID $FRONTEND_PID 2>/dev/null || true
  echo "✅ 服务已停止"
  exit 0
}

trap cleanup SIGINT SIGTERM

# 启动后端
echo "启动后端服务 (localhost:8080)..."
cd "$SCRIPT_DIR/server"
npm run dev &
BACKEND_PID=$!

sleep 2

# 启动前端
echo "启动前端开发服务 (localhost:5173)..."
cd "$SCRIPT_DIR"
npm run dev &
FRONTEND_PID=$!

echo ""
echo "========================================"
echo "✅ 服务已启动！"
echo "========================================"
echo ""
echo "📌 访问地址:"
echo "   前端: http://localhost:5173"
echo "   后端: http://localhost:8080"
echo "   API:  http://localhost:8080/api"
echo ""
echo "📋 测试账号:"
echo "   学生: stu1 / 123456"
echo "   老师: tea1 / 123456"
echo "   管理员: admin / 123456"
echo ""
echo "💡 提示: 按 Ctrl+C 停止服务"
echo ""

# 等待子进程
wait $BACKEND_PID $FRONTEND_PID 2>/dev/null || true

